package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.LinkedHashMap;
import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.Stat.AdPosition;
import cn.wit.summit.chwan.Stat.StatFactory;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.dto.ViewBean;
import cn.wit.summit.chwan.ui.fragment.FindFragment;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.MyImageLoader;
import cn.wit.summit.chwan.widgets.ItemListView;

public class FindFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //region ViewType
    public static int T_GAME_TYPE = 1;
    public static int T_TITLE     = 2;
    public static int T_GAME_ITEM = 3;
    public static int T_BILLBOARD = 4;
    //endregion

    private final Activity       context;
    private final List<ViewBean> datas;
    private final List<String>   gameList;

    public FindFragmentAdapter(Activity context, List<ViewBean> datas, List<String> gameList) {
        this.context = context;
        this.datas = datas;
        this.gameList = gameList;
    }

    //region Download Game
    private LinkedHashMap<String, String> downTemp = new LinkedHashMap<>();

    public void clearDownloadStatus() {
        downTemp.clear();
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size() > 0 && downTemp.containsKey(gameId)) {
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (getItemViewType(itemPo) == T_GAME_ITEM) {
            GameListBaseBean list = (GameListBaseBean) getItem(itemPo);
            return list;
        }
        return null;
    }
    //endregion

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == T_GAME_TYPE) {
            holder = new GameTypeVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_game_type_item, parent, false));
        } else if (viewType == T_TITLE) {
            holder = new TitleVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_title_item1, parent, false));
        } else if (viewType == T_GAME_ITEM) {
            holder = new GameItemVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_game_item_item, parent, false));
        } else if (viewType == T_BILLBOARD) {
            holder = new BillboardVH(LayoutInflater.from(context).inflate(R.layout.find_fragment_billboard_item, parent, false));
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder == null) return;

        int viewType = getItemViewType(position);
        if (viewType == T_GAME_TYPE) {
            onBindGameTypeViewHolder((GameTypeVH) holder, position);
        } else if (viewType == T_TITLE) {
            onBindTitleViewHolder((TitleVH) holder, position);
        } else if (viewType == T_GAME_ITEM) {
            onBindGameItemViewHolder((GameItemVH) holder, position);
        } else if (viewType == T_BILLBOARD) {
            onBindBillBoardViewHolder((BillboardVH) holder, position);
        }
    }

    private void onBindBillBoardViewHolder(BillboardVH holder, int position) {
        final JumpBaseBean jumpBaseBean = (JumpBaseBean) getItem(position);
        if (jumpBaseBean==null){holder.itemView.setVisibility(View.GONE);return;}
        if (holder.itemView.getVisibility()!=View.VISIBLE)holder.itemView.setVisibility(View.VISIBLE);
        MyImageLoader.load(holder.img, jumpBaseBean.getJump_cover_big_pic());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.getInstance().intentTo(context, jumpBaseBean);
            }
        });
    }

    private void onBindGameItemViewHolder(GameItemVH holder, final int position) {
        final GameListBaseBean gameListBaseBean = (GameListBaseBean) getItem(position);
        if (gameListBaseBean==null){holder.itemView.setVisibility(View.GONE);return;}
        if (holder.itemView.getVisibility()!=View.VISIBLE)holder.itemView.setVisibility(View.VISIBLE);
        holder.listItem.setDataNotagInfo(context, gameListBaseBean);
        downTemp.put(gameListBaseBean.getGame_id(), position + "");

        holder.listItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = gameList.indexOf(gameListBaseBean.getGame_id());
                if (index<10)
                StatFactory.getInstance().sendClickAdvertSpacePage(v.getContext(), AdPosition.getModeByEnum(7), AdPosition.getNameByEnum(7), "", "", index+1);
            }
        });

    }


    private void onBindTitleViewHolder(TitleVH holder, int position) {
        JumpBaseBean jumpBaseBean = (JumpBaseBean) getItem(position);
        if (jumpBaseBean==null){holder.itemView.setVisibility(View.GONE);return;}
        if (holder.itemView.getVisibility()!=View.VISIBLE)holder.itemView.setVisibility(View.VISIBLE);
        holder.title.setText(jumpBaseBean.getJump_sub_title());
    }

    private void onBindGameTypeViewHolder(GameTypeVH holder, final int position) {
        final FindFragment.DiscoveryType type = (FindFragment.DiscoveryType) getItem(position);
        if (type == null || type.getTypeId() == -1) {
            holder.icon.setVisibility(View.INVISIBLE);
            holder.name.setVisibility(View.INVISIBLE);
            return;
        }
        holder.icon.setVisibility(View.VISIBLE);
        holder.name.setVisibility(View.VISIBLE);
//        MyImageLoader.load(holder.icon, type.getResId(), "");
        holder.icon.setImageURI(MyImageLoader.makeUri(context, type.getResId()));
        holder.name.setText(type.getName());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.getTypeId() == -1) return;
                IntentUtil.getInstance().goDiscoveryListActivity(v.getContext(), type.getTypeId(), type.getName());
                StatFactory.getInstance().sendClickAdvertSpacePage(v.getContext(), AdPosition.getModeByEnum(6), AdPosition.getNameByEnum(6), "", "", position + 1);
            }
        });
    }

    Object getItem(int position) {
        if (datas == null) return null;
        return datas.get(position).getBean();
    }

    @Override
    public int getItemViewType(int position) {
        if (datas == null) return 0;
        return datas.get(position).getViewType();
    }

    @Override
    public int getItemCount() {
        if (datas == null) return 0;
        return datas.size();
    }

    private class GameTypeVH extends RecyclerView.ViewHolder {
        public SimpleDraweeView icon;
        public TextView         name;

        public GameTypeVH(View inflate) {
            super(inflate);
            icon = inflate.findViewById(R.id.icon);
            name = inflate.findViewById(R.id.name);
        }
    }

    private class TitleVH extends RecyclerView.ViewHolder {
        public TextView title;

        public TitleVH(View inflate) {
            super(inflate);
            title = inflate.findViewById(R.id.title);
        }
    }

    private class GameItemVH extends RecyclerView.ViewHolder {
        public ItemListView listItem;

        public GameItemVH(View inflate) {
            super(inflate);
            listItem = inflate.findViewById(R.id.listItem);
        }
    }

    private class BillboardVH extends RecyclerView.ViewHolder {
        public SimpleDraweeView img;

        public BillboardVH(View inflate) {
            super(inflate);
            img = inflate.findViewById(R.id.img);
        }
    }
}
